package cn.edu.xhu.library_management.user.service.impl;

import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import cn.edu.xhu.library_management.user.entity.LmUserStudent;
import cn.edu.xhu.library_management.user.entity.LmUserOhter;
import cn.edu.xhu.library_management.user.entity.search.OtherSearch;
import cn.edu.xhu.library_management.user.entity.search.StudentSearch;
import cn.edu.xhu.library_management.user.entity.search.TeacherSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.xhu.library_management.user.entity.LmUserOhter;
import cn.edu.xhu.library_management.user.service.LmUserOhterService;
import cn.edu.xhu.library_management.user.mapper.LmUserOhterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author 21086
* @description 针对表【lm_user_ohter(校外用户)】的数据库操作Service实现
* @createDate 2023-04-18 08:57:28
*/
@Service
public class LmUserOhterServiceImpl extends ServiceImpl<LmUserOhterMapper, LmUserOhter>
    implements LmUserOhterService{
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Override
    public Page<LmUserOhter> search(OtherSearch search) {
        Page<LmUserOhter> page = new Page<>(search.getCurrentPage(), search.getPageSize());
        QueryWrapper<LmUserOhter> wrapper = searchCondition(search);
        return this.page(page,wrapper);
    }

    @Override
    public boolean addOtherUser(LmUserOhter userOther) {

        // 查询是否存在当前用户
        if (this.getById(userOther.getOthAccount()) != null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"注册用户失败");
        }

        // 加密密码
        userOther.setOthPwd(encoder.encode(userOther.getOthPwd()));
        userOther.doDefault();
        return this.save(userOther);
    }

    /**
     * 搜索条件
     * @param search 搜索对象
     * @return 查询构造器
     */
    private QueryWrapper<LmUserOhter> searchCondition(OtherSearch search){
        QueryWrapper<LmUserOhter> wrapper = new QueryWrapper<>();
        if (search.getOthAccount() != null) {
            // 用户有账号
            wrapper.lambda().eq(LmUserOhter::getOthName, search.getOthName());
        }
        if (search.getOthName() != null) {
            // 姓名模糊查询
            wrapper.lambda().like(LmUserOhter::getOthName, search.getOthName());
        }
        if (search.getOthIslock() != null) {
            // 是否锁定
            wrapper.lambda().eq(LmUserOhter::getOthIslock, search.getOthIslock());
        }
        if (search.getOthIslock() != null) {
            // 是否锁定
            wrapper.lambda().eq(LmUserOhter::getOthIslock, search.getOthIslock());
        }

        if (search.getOthMinBalance() != null && search.getOthMaxBalance() != null){
            // 最小最大余额范围
            wrapper.lambda().between(LmUserOhter::getOthBalance,search.getOthMinBalance(),search.getOthMaxBalance());
        } else{
            if (search.getOthMinBalance() != null) {
                // 大于等于
                wrapper.lambda().ge(LmUserOhter::getOthBalance,search.getOthMinBalance());
            }else if (search.getOthMaxBalance() != null){
                // 小于等于
                wrapper.lambda().le(LmUserOhter::getOthBalance,search.getOthMaxBalance());
            }
        }
        if (search.getOthMinBorrowNum() != null && search.getOthMaxBorrowNum() != null){
            // 最小最大借阅范围
            wrapper.lambda().between(LmUserOhter::getOthBorrowNum,search.getOthMinBorrowNum(),search.getOthMaxBorrowNum());
        } else{
            if (search.getOthMinBorrowNum() != null) {
                // 大于等于
                wrapper.lambda().ge(LmUserOhter::getOthBorrowNum,search.getOthMinBorrowNum());
            }else if (search.getOthMaxBorrowNum() != null){
                // 小于等于
                wrapper.lambda().le(LmUserOhter::getOthBorrowNum,search.getOthMaxBorrowNum());
            }
        }


        return wrapper;
    }
}




